package palmtop.com.palmtop.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.adapter.HisTaskgridAdapter;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

public class HisgridtaskActivity extends BaseActivity {

    //    @BindView(R.id.mtitle)
//    TextView mtitle;
    @BindView(R.id.histaskgridList)
    ListView histaskgridList;
    @BindView(R.id.startdate)
    EditText startdate;
    @BindView(R.id.enddate)
    EditText enddate;
    @BindView(R.id.keyword)
    EditText keyword;
    @BindView(R.id.search)
    Button search;

    private ArrayList<Taskgrid> arrlist;
    private HisTaskgridAdapter adapter;
    //    private SharedPreferences sharedPreferences;
    String ssgrid = "";

    int mYear = 0;
    int mMonth = 0;
    int mDay = 0;

    int mYear1 = 0;
    int mMonth1 = 0;
    int mDay1 = 0;
    DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("-").append("0").
                            append(mMonth + 1).append("-").append("0").append(mDay).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("-").append("0").
                            append(mMonth + 1).append("-").append(mDay).append("").toString();
                }
            } else {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("-").
                            append(mMonth + 1).append("-").append("0").append(mDay).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("-").
                            append(mMonth + 1).append("-").append(mDay).append("").toString();
                }

            }
            startdate.setText(days);
        }
    };

    DatePickerDialog.OnDateSetListener onDateSetListener1 = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear1 = year;
            mMonth1 = monthOfYear;
            mDay1 = dayOfMonth;
            String days;
            if (mMonth1 + 1 < 10) {
                if (mDay1 < 10) {
                    days = new StringBuffer().append(mYear1).append("-").append("0").
                            append(mMonth1 + 1).append("-").append("0").append(mDay1).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear1).append("-").append("0").
                            append(mMonth1 + 1).append("-").append(mDay1).append("").toString();
                }
            } else {
                if (mDay1 < 10) {
                    days = new StringBuffer().append(mYear1).append("-").
                            append(mMonth1 + 1).append("-").append("0").append(mDay1).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear1).append("-").
                            append(mMonth1 + 1).append("-").append(mDay1).append("").toString();
                }

            }
            enddate.setText(days);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hisgridtask, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(HisgridtaskActivity.this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        arrlist = new ArrayList<Taskgrid>();
//        mtitle.setText("历史任务");
        AppPreferences appPreferences = new AppPreferences(HisgridtaskActivity.this);
//        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        ssgrid = appPreferences.getString("ssgrid", "");
        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);

        mYear1 = ca.get(Calendar.YEAR);
        mMonth1 = ca.get(Calendar.MONTH);
        mDay1 = ca.get(Calendar.DAY_OF_MONTH);
        startdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startdate.setInputType(InputType.TYPE_NULL);
                new DatePickerDialog(HisgridtaskActivity.this, onDateSetListener, mYear, mMonth, mDay).show();
            }
        });
        enddate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enddate.setInputType(InputType.TYPE_NULL);
                new DatePickerDialog(HisgridtaskActivity.this, onDateSetListener1, mYear1, mMonth1, mDay1).show();
            }
        });
        loadlist();
    }

    @OnClick(R.id.search)
    public void onViewClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskgridapp/taskgridlist");
        if (!TextUtils.isEmpty(startdate.getText())) {
            params.addBodyParameter("startdate",startdate.getText().toString());
        }
        if (!TextUtils.isEmpty(enddate.getText())) {
            params.addBodyParameter("enddate",enddate.getText().toString());
        }
        if (!TextUtils.isEmpty(keyword.getText())) {
            params.addBodyParameter("keyword",keyword.getText().toString());
        }
        params.addBodyParameter("unitid", ssgrid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<Taskgrid>>() {
                }.getType();
                ArrayList<Taskgrid> list = gson.fromJson(result, listType);
                adapter = new HisTaskgridAdapter(list, HisgridtaskActivity.this);
                histaskgridList.setAdapter(adapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    private void loadlist() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskgridapp/taskgridlist");
        params.addBodyParameter("unitid", ssgrid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<Taskgrid>>() {
                }.getType();
                ArrayList<Taskgrid> list = gson.fromJson(result, listType);
                adapter = new HisTaskgridAdapter(list, HisgridtaskActivity.this);
                histaskgridList.setAdapter(adapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }


}
